﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.ComponentModel;

namespace Pavilion
{
    public class Logger : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        static private NotifiableDictionary<string, Logger> loggerDictionary = new NotifiableDictionary<string, Logger>();

        public bool IsEnabled { get { return isEnabled; } set { isEnabled = value; OnPropertyChanged("IsEnabled"); } }
        public string Name { get; private set; }
        public string Data { get { return data; } set { data = value; OnPropertyChanged("Data"); } }

        private string data;
        private bool isEnabled;

        private Logger(string name)
        {
            this.Name = name;
            loggerDictionary.Add(name, this);
        }

        public void WriteLine(string data)
        {
            if (!IsEnabled)
                return;

            Data += data + Environment.NewLine;
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        static public Logger CreateLogger(string name) { try { return new Logger(name); } catch { return GetLogger(name); } }
        static public Logger GetLogger(string name) { return loggerDictionary[name]; }
        static public IEnumerable<Logger> GetAllLogs() { return loggerDictionary.ValueCollection; }
        static public void WriteLine(string loggerName, string data) { GetLogger(loggerName).WriteLine(data); }
    }
}
